from django.shortcuts import render
from .models import OrgInfo, CityInfo, TeacherInfo
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from operations.models import UserLove
from django.db.models import Q


# Create your views here.
def org_list(request):
    all_orgs = OrgInfo.objects.all()
    all_city = CityInfo.objects.all()
    # （love_num）   升序   从小到大
    # Test.objects.all().order_by('-love_num')   降序   从大到小

    # 全局搜索
    keyword = request.GET.get("keyword","")
    if keyword:
        # __icontains包含什么（不区分大小写）
        all_orgs = all_orgs.filter(
            Q(name__icontains=keyword) | Q(desc__icontains=keyword) | Q(detail__icontains=keyword))

    sort_orgs = all_orgs.order_by("-love_num")[:3]
    cate = request.GET.get("cate", "")
    # 按照机构筛选
    if cate:
        all_orgs = all_orgs.filter(category=cate)
    # 按照地区筛选
    cityid = request.GET.get("cityid", "")
    if cityid:
        all_orgs = all_orgs.filter(cityinfo_id=cityid)
    sort = request.GET.get("sort", "")
    if sort:
        all_orgs = all_orgs.order_by("-" + sort)

    # 分页功能
    pagenum = request.GET.get("pagenum", "")
    pa = Paginator(all_orgs, 3)
    try:
        pages = pa.page(pagenum)
    except PageNotAnInteger:
        pages = pa.page(1)
    except EmptyPage:
        pages = pa.page(pa.num_pages)

    # print(pagenum)
    # print(pa)
    # print(pages)

    return render(request, "orgs/org-list.html", {
        "all_orgs": all_orgs,
        "all_city": all_city,
        "sort_orgs": sort_orgs,
        "pages": pages,
        "cate": cate,
        "cityid": cityid,
        "sort": sort,
        "keyword": keyword,
        "tp": "d"
    })


def org_detail(request, org_id):
    if org_id:
        org = OrgInfo.objects.filter(id=int(org_id))[0]
        org.click_num += 1
        org.save()
        # 既然在base里添加了  那么子页面都需要加
        lovestatus = False
        if request.user.is_authenticated:
            love = UserLove.objects.filter(love_man=request.user, love_id=int(org_id), love_type=1, love_statu=True)
            if love:
                lovestatus = True
            else:
                lovestatus = False

        return render(request, "orgs/org-detail-homepage.html", {
            "org": org,
            "detail_type": "home",
            "lovestatus": lovestatus,
        })


def org_detail_course(request, org_id):
    if org_id:
        org = OrgInfo.objects.filter(id=int(org_id))[0]
        lovestatus = False
        if request.user.is_authenticated:
            love = UserLove.objects.filter(love_man=request.user, love_id=int(org_id), love_type=1, love_statu=True)
            if love:
                lovestatus = True
            else:
                lovestatus = False
        all_courses = org.coursesinfo_set.all()
        pagenum = request.GET.get("pagenum", "")
        pa = Paginator(all_courses, 2)
        try:
            pages = pa.page(pagenum)
        except PageNotAnInteger:
            pages = pa.page(1)
        except EmptyPage:
            pages = pa.page(pa.num_pages)
        return render(request, "orgs/org-detail-course.html", {
            "org": org,
            "pages": pages,
            "detail_type": "course",
            "lovestatus": lovestatus,

        })


def org_detail_desc(request, org_id):
    if org_id:
        org = OrgInfo.objects.filter(id=int(org_id))[0]
        lovestatus = False
        if request.user.is_authenticated:
            love = UserLove.objects.filter(love_man=request.user, love_id=int(org_id), love_type=1, love_statu=True)
            if love:
                lovestatus = True
            else:
                lovestatus = False
        return render(request, "orgs/org-detail-desc.html", {
            "org": org,
            "detail_type": "desc",
            "lovestatus": lovestatus,
        })


def org_detail_teacher(request, org_id):
    if org_id:
        org = OrgInfo.objects.filter(id=int(org_id))[0]
        lovestatus = False
        if request.user.is_authenticated:
            love = UserLove.objects.filter(love_man=request.user, love_id=int(org_id), love_type=1, love_statu=True)
            if love:
                lovestatus = True
            else:
                lovestatus = False
        return render(request, "orgs/org-detail-teachers.html", {
            "org": org,
            "detail_type": "teacher",
            "lovestatus": lovestatus,
        })


def teacher_list(request):
    all_teacher = TeacherInfo.objects.all()
    sort_teacher = all_teacher.order_by("-age")[:2]
    keyword = request.GET.get("keyword","")
    if keyword:
        # __icontains包含什么（不区分大小写）
        all_teacher = all_teacher.filter(name__icontains=keyword)

    sort = request.GET.get("sort", "")
    if sort:
        all_teacher = all_teacher.order_by("-age")

    # 分页功能
    pagenum = request.GET.get("pagenum", "")
    pa = Paginator(all_teacher, 3)
    try:
        pages = pa.page(pagenum)
    except PageNotAnInteger:
        pages = pa.page(1)
    except EmptyPage:
        pages = pa.page(pa.num_pages)
    # print(pagenum)
    # print(pa)
    # print(pages)
    return render(request, "orgs/teachers-list.html", {
        "all_teacher": all_teacher,
        "sort_teacher": sort_teacher,
        "pages": pages,
        "keyword": keyword,
        "tp": "c",
    })


def teacher_detail(request, teacher_id):
    if teacher_id:
        teacher = TeacherInfo.objects.filter(id=int(teacher_id))[0]
        all_teacher = TeacherInfo.objects.all()
        sort_teacher = all_teacher.order_by("-age")[:2]
        loveteacher = False
        loveorg = False
        # lovecourses和loveorg 用来存储用户收藏这个东西的状态，在模板加载的时候来判断
        if request.user.is_authenticated:
            love = UserLove.objects.filter(love_id=int(teacher_id), love_type=3, love_statu=True, love_man=request.user)
            if love:
                loveteacher = True
            love1 = UserLove.objects.filter(love_id=int(teacher.work_company.id), love_type=1, love_statu=True,
                                            love_man=request.user)
            if love1:
                loveorg = True
        return render(request, "orgs/teacher-detail.html", {
            "teacher": teacher,
            "sort_teacher": sort_teacher,
            "loveteacher": loveteacher,
            "loveorg": loveorg,
            "tp": "c",
        })
